// subpages0/newyearGreeting/newyearGreeting.js
var app = getApp();
var bgMusicCtx = wx.createInnerAudioContext();
Page({
  data: {

  },
  onLoad: function (e) {
    var that = this;
    if(e&&e.id){
      that.setData({
        blessingId:e.id
      })
    }
    if (!app.globalData.plumSession) {
      app.wechatSq(that);
    } else {
      that.requestBlesscfg();
      that.getSlient();
    }
  },
  onShow: function () {

  },
  onHide: function () {
    var that = this;
    bgMusicCtx.pause();
    that.setData({
      isPlaymusic: false
    })
  },
  onUnload: function () {
    var that = this;
    bgMusicCtx.stop();
    that.setData({
      isPlaymusic: false
    })
  },
  initPage: function(){
    var that = this;
    that.onLoad();
    that.onShow();
  },
  getSlient:function(){
    var that = this;
    var slient = app.globalData.slient ? app.globalData.slient:'';
    that.setData({
      slient: slient
    })
    if (slient == 1) {
      wx.hideTabBar()
    }
  },
  getBgmusic: function (e) {
    var that = this;
    var blessCfg = that.data.blessCfg;
    if (blessCfg.music) {
      bgMusicCtx.src = blessCfg.music;
      bgMusicCtx.loop = true;
      bgMusicCtx.play();
      that.setData({
        isPlaymusic: true
      })
    }
  },
  togglePlaymusic: function () {
    var that = this;
    var isPlaymusic = that.data.isPlaymusic;
    if (isPlaymusic) {
      bgMusicCtx.pause();
      isPlaymusic = false;
    } else {
      bgMusicCtx.play();
      isPlaymusic = true;
    }
    that.setData({
      isPlaymusic: isPlaymusic
    })
  },
  requestBlesscfg: function () {
    var that = this;
    //发起请求，获取列表列表
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: {
        map: 'applet_blessing_cfg',
        blessingId: that.data.blessingId ? that.data.blessingId : ''
      },
      success: function (res) {
        if (res.data.ec == 200) {
          console.log(res.data.data);
          var blessCfg = res.data.data;
          that.setData({
            blessCfg: blessCfg,
            blessingId: blessCfg.blessingId
          })
          if (blessCfg.title) {
            wx.setNavigationBarTitle({
              title: blessCfg.title
            });
          }
          that.getBgmusic();
        } else {
          app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
  backShouye: function () {
    app.backHome();
  },
  toeditGreet:function(){
    var that = this;
    var blessingId = that.data.blessingId ? that.data.blessingId:''
    wx.navigateTo({
      url: '/subpages0/yearGreeting/editGreeting/editGreeting?id=' + blessingId,
    })
  },
  showSharemodal: function () {
    this.setData({
      isShowshare: true,
      isShowbargainprice: false
    })
  },
  hideSharemodal: function () {
    this.setData({
      isShowshare: false
    })
  },
  peiviewPoster: function () {
    var that = this;
    var blessingId = that.data.blessingId ? that.data.blessingId : '';
    var mid = app.globalData.userInfo.mid;
    var data = {
      map: 'applet_blessing_share',
      id: blessingId,
      mid: mid
    }
    console.log(data);
    wx.showLoading({
      title: '海报生成中',
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        console.log(res.data);
        if (res.data.ec == 200) {
          var detailInfo = res.data.data;
          var curimg = detailInfo.shareImg;
          var imgs = [];
          imgs.push(curimg);
          wx.previewImage({
            current: curimg, // 当前显示图片的http链接
            urls: imgs // 需要预览的图片http链接列表
          })
        } else {
          app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        wx.hideLoading();
        wx.stopPullDownRefresh();
      }
    });
  },
  onShareAppMessage: function () {
    var that = this;
    var blessCfg = that.data.blessCfg;
    var title = blessCfg.blessNickname + ' ' + blessCfg.defaultName;
    var blessingId = that.data.blessingId ? that.data.blessingId: '';
    var mid = app.globalData.userInfo.mid;
    return {
      title: title,
      path: '/subpages0/yearGreeting/shopGreeting/shopGreeting?from=user&mid=' + mid +'&id=' + blessingId
    }
  }
})